﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AccountingLibrary
{
    public class GeneralLedgerCollection : System.Collections.Generic.List<GeneralLedger>
    {
        // constructors
        public GeneralLedgerCollection(AccountBase accountforjournal,IJournalable journals)
        {
            this.account = accountforjournal;
            this.journal_entities = journals;
        }
        public GeneralLedgerCollection(AccountBase accountforjournal, IJournalable journals,DateTime from ,DateTime to)
        {
            this.account = accountforjournal;
            this.journal_entities = journals;
            get_general_ledger(from, to);
        }
        // properties
        private DateTime from_date;
        private DateTime to_date;
        private IJournalable journal_entities { get; set; }
        public double journallast { get; set; }
        private AccountBase account;
      // methodes
        public virtual void get_general_ledger(DateTime From, DateTime To)
        {
            this.from_date = From;
            this.to_date = To;
            frist_general_ledger();
            this.AddRange(get_journal_entity());
            last_general_ledger();  
        }
       protected virtual void frist_general_ledger()
        {
            double fristvalue = get_frist_value();
            GeneralLedger gl = new GeneralLedger("أول الفترة");
            if ((bool)account.type) gl.ToValue = fristvalue;
            else gl.FromValue = fristvalue;
            this.Add(gl);
        }

       protected virtual List<GeneralLedger> get_journal_entity()
       {
         return  journal_entities.get_journals(account, from_date, to_date).ConvertAll<GeneralLedger>(new Converter<JournalEntityBase, GeneralLedger>(journal2general));
       }
       protected virtual double get_frist_value()
       {
           return(from details in account.accountdetails where details.frist_value_date.Date == from_date.Date select details.frist_value).FirstOrDefault();
       }
      protected  GeneralLedger journal2general(JournalEntityBase entity)
       {
           JournalEntityDetailBase cc = (from fff in entity.journaldetails where fff.account.name == account.name select fff).FirstOrDefault();
           GeneralLedger converted = new GeneralLedger();
           converted.FromValue =cc.debit;
           converted.ToValue=cc.credit;
           if (cc.credit == null || cc.credit == 0) converted.journaldetails = (from fff in entity.journaldetails where fff.debit != 0 || fff.debit != null select fff).ToList();
           return converted;
       }

      protected virtual void last_general_ledger()
        {
            double tosum = this.Sum(x => x.ToValue);
            double fromsum = this.Sum(x => x.FromValue);
            GeneralLedger lv = new GeneralLedger("أخر الفترة");
            if ((bool)account.type)
            {
                lv.FromValue = tosum - fromsum;
                journallast = lv.FromValue;

            }
            else
            {
                lv.ToValue = fromsum - tosum;
                journallast = lv.ToValue;
            }
            this.Add(lv);

        }
    }
}
